package com.dc.client.controller;

import com.dc.client.constant.QueueDict;
import com.dc.client.entity.UserEntity;
import com.dc.client.service.UserService;
import com.dc.client.util.MqSender;
import com.dc.client.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 测试消息队列
 * @auther: 赵聪
 * @date: 2018/8/13 17:09
 */
@RestController
public class MqController {
    private static final int NUM = 200;
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(8);
    private static CountDownLatch cdl = new CountDownLatch(NUM);


    @RequestMapping("/test")
    public String test() {

        for (int i = 0; i < NUM; i++) {
            fixedThreadPool.execute(new clientThread());
            cdl.countDown();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "成功";
    }

    public static class clientThread implements Runnable {
        MqSender mqSender = (MqSender) SpringContextUtils.getBean("mqSender");
        @Override
        public void run() {
            try {
                cdl.await();
                mqSender.send(QueueDict.QueueName_AutoAudit_Step0_Ready, "好啊");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
